package tt.dz.dao.ext.system;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections.map.ListOrderedMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Repository;

import tt.dz.dao.base.BaseDao;
import tt.dz.entity.base.OmsFactoryList;
import tt.dz.entity.system.OmsOperList;
import tt.dz.service.imp.middleware.LoginServiceImpl;
import tt.dz.util.Constants;

/**
 * 用户登录数据操作dao
 * @author pengyu
 * @date 2015年12月16日
 */
@Repository
public class OmsLoginDao extends BaseDao{
	
	private Log log = LogFactory.getLog(OmsLoginDao.class);
//	public  OmsOperList checkUser(String operName){
//		OmsOperList obj_query =  new OmsOperList();
//		obj_query.setOper_name(operName);
//		OmsOperList omsOperList =  this.findOneExample(obj_query,null,null);
//		return omsOperList;
//	}	
	public  OmsFactoryList checkUser(String fac_login_name){
		OmsFactoryList obj_query =  new OmsFactoryList();
		obj_query.setFac_login_name(fac_login_name);
		OmsFactoryList factoryList;
		try {
			factoryList = (OmsFactoryList) this.findOneExample(obj_query,null,null);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return factoryList;
	}	
	
	
	
//	public  OmsOperList checkUserAndPwd(String operName,String password){
//		OmsOperList obj_query =  new OmsOperList();
//		obj_query.setOper_name(operName);
//		obj_query.setPassword(password);
//		OmsOperList omsOperList =  this.findOneExample(obj_query,null,null);
//		return omsOperList;
//	}
	
	public  OmsFactoryList checkUserAndPwd(String fac_login_name,String fac_password){
		OmsFactoryList obj_query =  new OmsFactoryList();
		obj_query.setFac_login_name(fac_login_name);
		obj_query.setFac_password(fac_password);
		OmsFactoryList factoryList =  (OmsFactoryList) this.findOneExample(obj_query,null,null);
		return factoryList;
	}
	
	public  String checkChargerByIp(String source_ip,String source_port){
		List<Map<String, Object>>  list = this.findDataBySql("select charger_id from oms_charger WHERE source_ip='"+source_ip+"' and source_port='"+source_port+"' "
				+ "and UNIX_TIMESTAMP( now( ) ) - UNIX_TIMESTAMP( sys_creat_time ) <1800 ORDER by charger_id DESC LIMIT 0,1", null);
		if(list.size()==0){
			return null;
		}
		Map<String, Object> map = list.get(0);
		return map.get("key_link").toString();
	}
	
	/**
	 * 分页查询操作员信息
	 * @param paramsMap 有序参数Map
	 * @param pageNo 页码索引
	 * @param pageSize 每页个数
	 * @param sortName 排序列名
	 * @param sortVal 排序方式 1 正序 其他倒序
	 * @return
	 */
	public List<OmsOperList> pagingFindValidOperEmbedList(
			ListOrderedMap paramsMap, Integer pageNo, Integer pageSize, String sortName,
			Integer sortValue){
		
		if (pageNo!=null && pageNo <= 1) {
			pageNo = 1;
		}
		// 如果参数集没有初始化
		if (paramsMap == null) {
			paramsMap = new ListOrderedMap();
		}
		// 设置有效标志
		paramsMap.put(Constants.Jdbc.VALID_FLAG, 1);
		
		return super.findListByClass(OmsOperList.class, paramsMap, pageNo, pageSize, sortName, sortValue);
	}
	
	/**
	 * 通过entitytype 和 entityid分页查询有效归属操作员列表
	 * @author Administrator
	 * @param entity_type
	 * @param entity_id
	 * @param pageNo 页码索引
	 * @param pageSize 每页个数
	 * @param sortName 排序列名
	 * @param sortVal 排序方式 1 正序 其他倒序
	 * @return
	 */
	public List<OmsOperList> pagingFindValidAttributionOperByEntityTypeAndEntityIdEmbedList(
			Integer entity_type,String entity_id,Integer pageNo, Integer pageSize, 
			String sortName,Integer sortValue){
		
		OmsOperList operList = new OmsOperList();
		
		//设置查询参数
		operList.setEntity_type(entity_type);
		operList.setEntity_id(entity_id);
		operList.setValid_flag(1);
		
		return super.findExample(operList, pageNo, pageSize, sortName, sortValue);
	}
	
	/**
	 * 查询操作员详细包含无效操作员
	 * @author Administrator
	 * @param oper_id
	 * @return
	 */
	public OmsOperList findOperDetailByOperIdIncludeInvalid(String oper_id){
		OmsOperList operList = new OmsOperList();
		//设置operid
		operList.setOper_id(oper_id);
		List<OmsOperList> omsOperLists = super.findExample(operList, null, null, null, null);
		if(omsOperLists!=null && omsOperLists.size()>0){
			return omsOperLists.get(0);
		}
		return null;
	}
	
	/**
	 * 删除操作员信息
	 * @author Administrator
	 * @param oper_id
	 * @return
	 */
	public int logicDeleteOperByOperId(String oper_id){
		
		OmsOperList operList = new OmsOperList();
		//设置operid
		operList.setOper_id(oper_id);
		return super.LogicDeleteObject(operList);
	}



	public OmsFactoryList checkFactory(String facCode) {
		OmsFactoryList factoryList;
		try {
			factoryList = (OmsFactoryList) this.findOneExampleByCondition(new OmsFactoryList(), " where fac_login_name like '%"+facCode+"'");
		} catch (Exception e) {
			e.printStackTrace();
			log.error(" login facCode null");
			return null;
		}
		return factoryList;
	}
	
}